var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const cors = require('cors');

require('dotenv').config();
const adminAuth = require('./middlewares/admin-auth');
const userAuth = require('./middlewares/user-auth');
const categoriesRouter = require('./routes/categories');
const coursesRouter = require('./routes/courses');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var articlesRouter = require('./routes/admin/articles');
const adminCategoriesRouter = require('./routes/admin/categories');
const adminSettingsRouter = require('./routes/admin/settings');
const adminUsersRouter = require('./routes/admin/users');
const adminCoursesRouter = require('./routes/admin/courses');
const adminChaptersRouter = require('./routes/admin/chapters');
const adminChartsRouter = require('./routes/admin/charts');
const adminAuthRouter = require('./routes/admin/auth');
var app = express();

app.use(cors());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//前台路由配置
app.use('/',indexRouter);
app.use('/users', userAuth,usersRouter);
app.use('/categories',adminAuth, categoriesRouter);
app.use('/courses', coursesRouter);
//后台路有配置
app.use('/admin/articles', adminAuth,articlesRouter);
app.use('/admin/categories', adminAuth,adminCategoriesRouter);
app.use('/admin/settings',adminAuth, adminSettingsRouter);
app.use('/admin/users',adminAuth, adminUsersRouter);
app.use('/admin/courses', adminAuth,adminCoursesRouter);
app.use('/admin/chapters', adminAuth,adminChaptersRouter);
app.use('/admin/charts', adminAuth,adminChartsRouter);
app.use('/admin/auth',adminAuthRouter);
module.exports = app;
